package com.justenholter.text.effects {
	
	import com.justenholter.text.effects.IEffect;
	
	/**
	 * Overwrites the curText with the newText.
	 * 
	 * @author justen holter
	 */
	public class OverType implements IEffect {
		
		/**
		 * Creates a new instance of OverType.
		 */
		public function OverType() {
			
		}
		
		/**
		 * @inheritDoc
		 */
		public function init(curText:String, newText:String):String {
			return next(curText, newText);
		}
		
		/**
		 * @inheritDoc
		 */
		public function next(curText:String, newText:String):String {
			
			var temp:String = curText;
			
			for(var i:Number = 0; i < newText.length; i++){
				
				var curChar:String = curText.charAt(i);
				var newChar:String = newText.charAt(i);
				
				if(curChar != newChar){
					
					/* break temp up into two pieces leaving out
					 * the curChar and inserting newChar between them */
					var a:String = temp.slice(0, i) + newChar;
					var b:String = temp.slice(i + 1, temp.length);
					
					return a == newText ? a : String(a + b);
				}
			}
			
			return newText;
		}
	}
}